#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

//void* thread_run(void* args){
//  while(1){
//    printf("new thread id: 0x%x\n",pthread_self());
//    sleep(2);
//  }
//}
//int main(){
//  pthread_t tid;
//  pthread_create(&tid, NULL, thread_run, (void*)"new thread");
//  while(1){
//    printf("main thread id: 0x%x\n",pthread_self());
//    sleep(1);
//  }
//  return 0;
//}





void* thread_run(void* args){
  pthread_detach(pthread_self());
  int num = *(int*)args;
  while(1){
    printf("我是新线程[%d], 我创建的线程ID是：%lu\n", num, pthread_self());
    sleep(2);
    break;

    //pthread_exit((void*)123);//终止线程
    //break;
    //if(num == 3){
    //  printf("thread number : %d quit\n", num);
    //  int* p =NULL;
    //  *p = 100;
    //}
  }
  return (void*)111;
}

#define NUM 1
int main(){
  pthread_t tid[NUM];
  for(int i =0; i< NUM; i++){
    pthread_create( tid + i, NULL, thread_run, (void*)&i);
    sleep(1);
  }

  printf("wait sub thread ...\n");
  sleep(5);

  //printf("cancel sub thread ...\n");
  //pthread_cancel(tid[0]);//取消线程

  void* status = NULL;
  int ret =0;
  for(int i =0; i < NUM; i++){
    ret = pthread_join(tid[i], &status);//获取线程退出信息
  }
  printf("ret: %d, status %d\n", ret, (int)status);
  printf("wait success!\n");

  sleep(1);

 // while(1){
 //   printf("我是主线程，我的thread ID： %lu\n", pthread_self());
 //   printf("##########################begin###############\n");
 //   for(int i  = 0; i<NUM; i++){
 //     printf("我创建的线程[%d]是：%lu\n", i, tid[i]);
 //   }
 //   printf("##########################end#################\n");
 //   sleep(1);
 // }
  return 0;
}
